<?php
include_once('admin.inc.php');
$catModel = new Model('category');
$smarty = new Tpl();

$act = get_param('do');
switch( $act ){
	default:
	case 'categories':
		// 分类
		$catId = get_param('id');
		$where = "1";
		if( $catId ){
			$where = "A.id=$catId";
		}
		$sql = "SELECT A.*, B.name AS pname FROM fl_category A LEFT JOIN fl_category B ON A.pid=B.id WHERE $where ORDER BY A.pid, A.id";
		$rs = $catModel->query($sql);
		$list = $catModel->get();
		if( DEBUG ){
			echo $catModel->get_sql();
			dump($list);
		}

		$smarty->assign('list',$list);
		$smarty->display('admin/categories.html');
	break;
	case 'addCat':
		$data = get_data();
		$data['edittime'] = time();
		unset($data['id']);
		if( empty($data['name']) ){
			msg('分类名称不能为空',-1);
		}
		$rsCat = $catModel->value($data)->add();
		if( !$rsCat ){
			msg('添加失败'.$catModel->get_sql(), 'category.php');
		}
		msg('添加成功','category.php');
		
	break;
	case 'editCat':
		$id = intval(get_param('id'));
		if( empty($id) ){
			msg('非法id',-1);
		}
		$data = get_data();

		$rsCat = $catModel->where("id=$id")->value($data)->update();
		if( $rsCat ){
			msg('修改成功','category.php');
		}else{
			msg('修改失败');
		}
	break;
	case 'delCat':
		$id = intval(get_param('id'));
		if( empty($id) ){
			msg('非法id',-1);
		}
		$rs = $catModel->where("id=$id")->del();
		if( $rs ){
			ajax_return(array('status'=>'success','info'=>'删除成功'));
		}else{
			ajax_return(array('status'=>'failed','info'=>'删除失败'));
		}
		
	break;
}
